/*
 * 按钮
 */

#include "gui.h"
#include <stdlib.h>
#include <string.h>

enum Button_status {
	BS_NONE,
	BS_PRESSED,
	BS_OVER
};


void ACALLBACK __button_mouse_down(struct Area *who,
				int button, int x, int y, 
				void *userdata)
{
	struct Button *bt = (struct Button *) userdata;
	bt -> _status = BS_PRESSED;
}

void ACALLBACK __button_mouse_up(struct Area *who,
				   int button, int x, int y, 
				   void *userdata)
{
	struct Button *bt = (struct Button *) userdata;
	if (bt -> click && bt -> _status == BS_PRESSED) {
		(*bt -> click)(bt, bt -> userdata);
	}
	bt -> _status = BS_OVER;
}

void ACALLBACK __button_mouse_enter(struct Area *who, void *userdata)
{
	struct Button *bt = (struct Button *) userdata;
	bt -> _status = BS_OVER;
	if (bt -> enter)
		(*bt -> enter)(bt, bt -> userdata);
}

void ACALLBACK __button_mouse_leave(struct Area *who, void *userdata)
{
	struct Button *bt = (struct Button *) userdata;
	bt -> _status = BS_NONE;
	if (bt -> leave)
		(*bt -> leave)(bt, bt -> userdata);
}

void ACALLBACK __button_render(struct Area *who,
			       void *userdata)
{
	struct Button *bt = (struct Button *) userdata;
	switch(bt -> _status) {
		case BS_OVER:
			video_set_color(215, 215, 210);
			break;
		case BS_PRESSED:
			video_set_color(128, 128, 128);
			break;
		default:
			video_set_color(242, 242, 238);
			break;
	}
	int x, y;
	x = who -> _x;
	y = who -> _y;
	
	video_bar(x, y, x + who -> _width, y + who -> _height);
	video_set_color(0, 0, 0);
	video_rectangle(x, y, x + who -> _width, y + who -> _height);
	if (bt -> but_image) {
		video_drawimg(bt -> but_image, x, y);
	}
	if (bt -> _font && bt -> _caption) {
		Image a;
		a = ttf_make_img(bt -> _font, bt -> _caption, bt -> _font_color);
		int xx = x + (who -> _width - img_get_width(a)) / 2;
		int yy = y + (who -> _height - img_get_height(a)) / 2;
		video_drawimg(a, xx, yy);
		video_destory_img(a);
	}
}

struct Button *button_new(struct Panel *father, int x, int y,
			  int width, int height)
{
	struct Button *ret;
	ret = malloc(sizeof(struct Button));
	memset(ret, 0, sizeof(struct Button));
	
	struct Area *a;
	int xx, yy;
	if (father) {		//新建按钮使用的是相对坐标，这里转换成绝对坐标
		a = father -> _area;
		xx = father -> _area -> _x + x;
		yy = father -> _area -> _y + y;
	} else {
		a = NULL;
		xx = x;
		yy = y;
	}
	ret -> _area = area_new(a, xx, yy, width, height);
	ret -> _area -> enable = 1;
	ret -> _area -> visual = 1;
	ret -> _area -> userdata = ret;
	ret -> _area -> mouse_down = __button_mouse_down;
	ret -> _area -> mouse_up = __button_mouse_up;
	ret -> _area -> mouse_enter = __button_mouse_enter;
	ret -> _area -> mouse_leave = __button_mouse_leave;
	ret -> _area -> user_render = __button_render;
	
	ret -> _font = gui_get_font_small();

	return ret;
}

void button_set_caption(struct Button *bt, const char *cap)
{
	if (bt -> _caption) free(bt -> _caption);
	bt -> _caption = malloc(strlen(cap) + 2);
	strcpy(bt -> _caption, cap);
}

void button_set_font(struct Button *bt, Font font)
{
	bt -> _font = font;
}

void button_set_image(struct Button *bt, Image img)
{
	bt -> but_image = img;
}

void button_delete(struct Button *bt)
{
//	area_delete((father ? father -> _area : NULL),
//		    bt -> _area);
	bt -> _area -> enable = 0;
	bt -> _area -> visual = 0;
	if (bt -> _caption) free(bt -> _caption);
	free(bt);
}

void button_set_color(struct Button *bt, Uint32 color)
{
	bt -> _font_color = color;
}


void button_clicked(struct Button *bt, void ACALLBACK (*f)(struct Button *bt, void *userdata))
{
	bt -> click = f;
}
void button_enter(struct Button *bt, void ACALLBACK (*f)(struct Button *bt, void *userdata))
{
	bt -> enter = f;
}

void button_leave(struct Button *bt, void ACALLBACK (*f)(struct Button *bt, void *userdata))
{
	bt -> leave = f;
}

void button_set_userdata(struct Button *bt, void *userdata)
{
	bt -> userdata = userdata;
}

